<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('科目名称')" prop="keyName">
            <el-input v-model="form.keyName" :placeholder="$t('科目名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <span style="display: none;">key_code: {{ form.keyCode }}</span>
          <el-form-item :label="$t('财报类型')" prop="tableType">
            <el-select v-model="form.tableType" :placeholder="$t('财报类型')">
              <el-option
                v-for="dict in tableTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <!--
        <el-col :span="12">
          <el-form-item :label="$t('重点关注')" prop="focusOn">
            <el-radio-group v-model="form.focusOn" :placeholder="$t('重点关注')">
              <el-radio label="Y">{{ $t('是') }}</el-radio>
              <el-radio label="N">{{ $t('否') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('数据类型')" prop="keyType">
            <el-radio-group v-model="form.keyType" :placeholder="$t('数据类型')">
              <el-radio label="money">{{ $t('金额') }}</el-radio>
              <el-radio label="ratio">{{ $t('百分比') }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        -->
        <el-col :span="12">
          <el-form-item :label="$t('排序')" prop="keySort">
            <el-input v-model="form.keySort" :placeholder="$t('排序')" show-word-limit />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <el-button @click="cancel">{{ $t("取消") }}</el-button>
      <el-button v-hasPermi="['company:pcw:edit']" type="primary" @click="submitForm">{{ $t("保存") }}</el-button>
    </div>
  </div>
</template>

<script>
    import { savePcwTempSubject, getPcwTempSubject } from '@/api/company/pcw/pcwTempSubject'
    export default {
      props: {
        companyId: {
          type: String,
          required: true,
          default: ''
        }
      },
        data() {
            return {
                // 表类型字典
                tableTypeOptions: [],
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  tableType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  keyName: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }],
                  keySort: [{ required: true, message: this.$t('必填信息'), trigger: 'change' }]
                }
            }
        },
        created() {
            this.getDicts('pcw_table_type').then(response => {
                this.tableTypeOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                if (data !== null && data.id !== undefined) {
                    getPcwTempSubject(data.id).then(response => {
                        this.form = response.data
                    })
                } else {
                    this.reset()
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                companyId: this.companyId || this.$route.query.companyId,
                typeId: this.$route.query.typeId,
                keyName: undefined,
                keyCode: undefined,
                keySort: undefined,
                keyType: 'money',
                focusOn: 'N',
                tableType: this.form.tableType,
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                remark: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        savePcwTempSubject(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           }

        }

    }
</script>
